﻿$(function() {

    $(document).ready(function () {
        var initFirstName = $('#firstName').val();
        var initLastName = $('#lastName').val();
        
        $('.ball, .ball1').removeClass('stop');
        $('.trigger').click(function() {
            $('.ball, .ball1').toggleClass('stop');
        });
        $("#datepicker-button").datepicker({ dateFormat: 'dd-mm-yy' });
        $('select').selectpicker();
        $('#patientValid').hide();
        $('#lastName').change(function() {
            if (initFirstName == $('#firstName').val() && initLastName == $('#lastName').val()) {
                $('#patientValid').hide();
            } else {
                $('#patientValid').show();
            }
        });
        $('#firstName').change(function () {
            if (initFirstName == $('#firstName').val() && initLastName == $('#lastName').val()) {
                $('#patientValid').hide();
            } else {
                $('#patientValid').show();
            }
        });
        $('#patientValid').click(function (event) {
            event.preventDefault();
            alert('bip');
            $.post("/Home/UpdatePatient", { firstName: $("#firstName").val(), lastName: $("#lastName").val(), invoiceId: $("#invoiceId").val() })
                .done(function() {
                    $(location).attr('href', "/Home/Index/" + $("#invoiceId").val());
                })
                .fail(function() {
                    $(location).attr('href', "/Home/Index/" + $("#invoiceId").val());
                });
        });


        $("#addRow").click(function(event) {
            event.preventDefault();
            if ($("#toothPlaceholder").val() != undefined && $("#toothPlaceholder").val() != "") {
                $("#AddLoader").css("display", "block");
                var link = $("#addRow").attr("href");
                var date = $("#datepicker-button").val();
                var inami = $("#inamiPicker").val();
                var toothId = $("#toothPlaceholder").val();
                var invoiceId = $("#invoiceId").val();
                var firstname = $("#firstName").val();
                var lastname = $("#lastName").val();
                $.post(link, { dateStr: date, inamicode: inami, toothid: toothId, invoiceId: invoiceId, firstname: firstname, lastname: lastname }).done(function() {
                    $(location).attr('href', "/Home/Index/" + invoiceId);
                });
            }
        });

        $(".help-inline").hide();

        $("#AddInami").click(function(event) {
            event.preventDefault();
            var error = false;
            if ($("#Code").val() == "") {
                error = true;
            }
            if ($("#Label").val() == "") {
                error = true;
            }
            if (error) {
                return;
            }
            $.post("./Add", { code: $("#Code").val(), label: $("#Label").val(), prix1: $("#Prix1").val(), prix2: $("#Prix2").val(), prix3: $("#Prix3").val() })
                .done(function() {
                    $(location).attr('href', "./Add?success=true");
                })
                .fail(function() {
                    $(location).attr('href', "./Add?success=false");
                });
        });


        $("#EditInami").click(function(event) {
            event.preventDefault();
            $.post("./Edit", { code: $("#Code").val(), label: $("#Label").val(), prix1: $("#Prix1").val(), prix2: $("#Prix2").val(), prix3: $("#Prix3").val() })
                .done(function() {
                    $(location).attr('href', "../Edit?success=true");
                })
                .fail(function() {
                    $(location).attr('href', "../Edit?success=false");
                });
        });

        $("#DeleteInami").click(function(event) {
            event.preventDefault();
            if (window.confirm('Êtes-vous sûr de vouloire supprimer le code ' + $("#Code").val())) {
                $.post("./Delete", { code: $("#Code").val() })
                    .done(function() {
                        $(location).attr('href', "./Delete?success=true");
                    })
                    .fail(function() {
                        $(location).attr('href', "./Delete?success=false");
                    });
            }
        });

        $("#SelectEditInami").click(function(event) {
            event.preventDefault();
            var pathArray = window.location.href.split('/');
            var url = "";
            for (var i = 0; i < pathArray.length - 1; i++) {
                url += pathArray[i] + "/";
            }
            $(location).attr('href', url + "Edit/" + $("#Code").val());
        });

        $(".removeToken").click(function(event) {
            event.preventDefault();
            var id = event["currentTarget"].toString().split('#')[1];
            $("#loader" + id).css("display", "block");
            $.post("/Home/RemoveRow", { rowId: id })
                .done(function() {
                    location.reload();
                })
                .fail(function() {
                    location.reload();
                });
        });

        $(".removeLink").click(function(event) {
            event.preventDefault();
            var id = event["currentTarget"].toString().split('#')[1];
            //$("#loader" + id).css("display", "block");
            $.post("/InamiManager/Unlink", { parentId: $("#Code").val(), childId: id })
                .done(function() {
                    location.reload();
                })
                .fail(function() {
                    location.reload();
                });
        });

        $("#linkCode").click(function(event) {
            event.preventDefault();
            $.post("/InamiManager/Link", { parentId: $("#Code").val(), childId: $("#requiredSelect").val() })
                .done(function() {
                    location.reload();
                })
                .fail(function() {
                    location.reload();
                });
        });

        $(".btn-print").click(function(event) {
            event.preventDefault();
            if ($("#lastName").val() != "" && $("#firstName").val() != "") {
                $(location).attr('href', event.target);
            }
        });
    });
});

function selectionnerDent(id) {
    $("#tooth_picker_modal").modal('toggle');
    $("#toothPlaceholder").val(id);
}